﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace S7.Windows.Employees
{
    /// <summary>
    /// Interaction logic for AddAnEmployee.xaml
    /// </summary>
    public partial class AddAnEmployee : Window
    {
        public AddAnEmployee()
        {
            InitializeComponent();
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Employee emp = new Employee();
            emp.FirstName = Info.Capitalize(tbFirstName.Text);
            emp.LastName = Info.Capitalize(tbLastName.Text);
            emp.Address = Info.Capitalize(tbAddress.Text);
            emp.Mobile = Info.Capitalize(tbMobile.Text);
            emp.Salary = decimal.Parse(tbSalary.Text);
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        emp.Add();
                        this.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                                this.Close();
                            }));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }
                }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
